<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/28
 * Time: 14:58
 */

namespace Api\Controller;

use Think\Controller;


class MyController extends Controller
{


    /**
     * 获取个人ID
     */
    public function My()
    {
        $user_id = $this->get_customer_id();
        if ($user_id) {
            $result = M('user')->where("customer_id='$user_id'")->field(array('nickname', 'id', 'headimgurl'))->find();
            $this->ajaxreturn(array('state' => 1, 'data' => $result), 'json');
        } else {
            $this->ajaxreturn(array('state' => 0), 'json');
        }
    }

    /**
     * 登出
     */
    public function logout()
    {
        session_unset($_SESSION);
        session_destroy();
        if (isset($_COOKIE[session_name()])) {  //判断客户端的cookie文件是否存在,存在的话将其设置为过期.
            setcookie(session_name(), '', time() - 1, '/');
        }
        $this->ajaxreturn(array('data' => 1), 'json');
    }

    /**
     *我的-优惠券列表
     */
    public function couponList()
    {
        $type = I('get.type');//优惠券类型0未使用1已使用2已过期
//        $customer_id = $_SESSION['customer_id'];//当前登录用户id
        $customer_id = '20181011094823781242';

        if ($customer_id) {
            $where['uc.customer_id'] = $customer_id;

            //判断优惠券的是否过期
            $user_coupon = M('user_coupon')->alias('uc')
                ->field('c.coupon_id')
                ->join('meal_coupon as c on uc.coupon_id=c.coupon_id', 'left')
                ->where(array('c.use_end_time' => ['elt', time()], 'uc.customer_id' => $customer_id))
                ->select();
            if ($user_coupon) {
                foreach ($user_coupon as $value) {
                    //修改过期优惠券状态
                    M('user_coupon')->where(['coupon_id' => $value['coupon_id']])->setField(['status' => 2]);
                }
            }

            switch ($type) {
                case 0://未使用
                    $where['uc.status'] = 0;
                    $result = M('user_coupon')->alias('uc')
                        ->field('uc.customer_id,uc.coupon_id,uc.store_id,c.money,c.condition_money,c.use_start_time,c.use_end_time')
                        ->join('meal_coupon as c on uc.coupon_id=c.coupon_id', 'left')
                        ->where($where)
                        ->select();
                    $return = array('state' => 1, 'msg' => '获取未使用优惠券成功', 'data' => $result);
                    break;
                case 1://已使用
                    $where['uc.status'] = 1;
                    $result = M('user_coupon')->alias('uc')
                        ->field('uc.customer_id,uc.coupon_id,uc.store_id,c.money,c.condition_money,c.use_start_time,c.use_end_time')
                        ->join('meal_coupon as c on uc.coupon_id=c.coupon_id', 'left')
                        ->where($where)
                        ->select();
                    $return = array('state' => 1, 'msg' => '获取已使用使用优惠券成功', 'data' => $result);
                    break;
                case 2://已过期
                    $where['uc.status'] = 2;
                    $result = M('user_coupon')->alias('uc')
                        ->field('uc.customer_id,uc.coupon_id,uc.store_id,c.money,c.condition_money,c.use_start_time,c.use_end_time')
                        ->join('meal_coupon as c on uc.coupon_id=c.coupon_id', 'left')
                        ->where($where)
                        ->select();;
                    $return = array('state' => 1, 'msg' => '获取已过期优惠券成功', 'data' => $result);
                    break;
                default:
                    $where['uc.status'] = 0;
                    $result = M('user_coupon')->alias('uc')
                        ->field('uc.customer_id,uc.coupon_id,uc.store_id,c.money,c.condition_money,c.use_start_time,c.use_end_time')
                        ->join('meal_coupon as c on uc.coupon_id=c.coupon_id', 'left')
                        ->where($where)
                        ->select();
                    $return = array('state' => 1, 'msg' => '获取未使用优惠券成功', 'data' => $result);
            }

            if (!empty($result)) {
                $this->ajaxReturn($return);
            } else {
                $this->ajaxReturn(['state' => 0, 'msg' => '暂无数据']);
            }
        } else {
            $this->ajaxReturn(['state' => -1, 'msg' => '对不起请登录']);
        }
    }
}